package drds.global_transaction.api.message_codec.request_and_response.register_manager;

import drds.global_transaction.api.message_codec.RequestMessage;
import drds.global_transaction.api.message_codec.Version;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.nio.ByteBuffer;

@Slf4j
public abstract class RegisterManagerRequestMessage extends RequestMessage
{

	private static final long serialVersionUID = 1L;

	@Setter
	@Getter
	protected String version = Version.current;
	@Setter
	@Getter
	protected String applicationId;
	@Setter
	@Getter
	protected String transactionServiceGroup;
	@Setter
	@Getter
	protected String extraData;

	public RegisterManagerRequestMessage(String applicationId, String transactionServiceGroup)
	{
		this.applicationId = applicationId;
		this.transactionServiceGroup = transactionServiceGroup;
	}

	public RegisterManagerRequestMessage(String applicationId, String transactionServiceGroup, String extraData)
	{
		this.applicationId = applicationId;
		this.transactionServiceGroup = transactionServiceGroup;
		this.extraData = extraData;
	}

	@Override
	public boolean decode(ByteBuf byteBuf)
	{
		@SuppressWarnings("unused")
		int readableBytes = byteBuf.readableBytes();
		try
		{
			short length = byteBuf.readShort();
			if (length > 0)
			{
				byte[] bytes = new byte[length];
				byteBuf.readBytes(bytes);
				this.setVersion(new String(bytes, UTF8));
			}
			length = byteBuf.readShort();
			if (length > 0)
			{
				byte[] bytes = new byte[length];
				byteBuf.readBytes(bytes);
				this.setApplicationId(new String(bytes, UTF8));
			}
			length = byteBuf.readShort();
			if (length > 0)
			{
				byte[] bytes = new byte[length];
				byteBuf.readBytes(bytes);
				this.setTransactionServiceGroup(new String(bytes, UTF8));
			}
			length = byteBuf.readShort();
			if (length > 0)
			{
				byte[] bytes = new byte[length];
				byteBuf.readBytes(bytes);
				this.setExtraData(new String(bytes, UTF8));
			}
		} catch (Exception exx)
		{
			log.error(exx.getMessage() + this);
			return false;
		}
		if (log.isDebugEnabled())
		{
			log.debug(byteBuf.writerIndex() == byteBuf.readerIndex() ? "true" : "false" + this);
		}

		return true;
	}

//

	//
	public ByteBuffer putBytesToByteBuffer()
	{
		ByteBuffer byteBuffer = createByteBuffer();
		byteBuffer.clear();
		if (this.version != null)
		{
			byte[] bytes = version.getBytes(UTF8);
			byteBuffer.putShort((short) bytes.length);
			if (bytes.length > 0)
			{
				byteBuffer.put(bytes);
			}
		} else
		{
			byteBuffer.putShort((short) 0);
		}

		if (this.applicationId != null)
		{
			byte[] bytes = applicationId.getBytes(UTF8);
			byteBuffer.putShort((short) bytes.length);
			if (bytes.length > 0)
			{
				byteBuffer.put(bytes);
			}
		} else
		{
			byteBuffer.putShort((short) 0);
		}

		if (this.transactionServiceGroup != null)
		{
			byte[] bytes = transactionServiceGroup.getBytes(UTF8);
			byteBuffer.putShort((short) bytes.length);
			if (bytes.length > 0)
			{
				byteBuffer.put(bytes);
			}
		} else
		{
			byteBuffer.putShort((short) 0);
		}

		if (this.extraData != null)
		{
			byte[] bytes = extraData.getBytes(UTF8);
			byteBuffer.putShort((short) bytes.length);
			if (bytes.length > 0)
			{
				byteBuffer.put(bytes);
			}
		} else
		{
			byteBuffer.putShort((short) 0);
		}

		//
		markAsEncode();
		return byteBuffer;

	}
}
